﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MappingGenerator.Formatting;
using MappingGenerator.Template;

namespace MappingGenerator
{
    public class Generator
    {
        private IDbSchemaReader reader;
        private FileOutPut outPut;
        public IDbObjectNameFormatter Formatter { get; set; }
        private string _mappingClassNamespace = "Mappings";
        public string MappingClassNamespace
        {
            get { return _mappingClassNamespace; }
            set { _mappingClassNamespace = value; }
        }

        private string _entityClassNamespace = "Entities";
        public string EntityClassNamespace
        {
            get { return _entityClassNamespace; }
            set { _entityClassNamespace = value; }
        }

        public Generator(IDbSchemaReader dbSchemaReader)
        {
            reader = dbSchemaReader;
            outPut = new FileOutPut();
        }

        public string OutputFolder
        {
            get { return outPut.SavePath; }
            set
            {
                outPut.SavePath = value;
            }
        }

        public void GenerateEntity(string tableName)
        {
            var table = reader.GetTable(tableName);
            
            var template =  new EntityTemplate(table,Formatter);
            template.Namespace = EntityClassNamespace;
            outPut.Render(template);
        }
        public void GenerateAll(List<string> tableNames)
        {
            var mappingBaseTemplate = new MappingBaseTemplate();
            mappingBaseTemplate.Namespace = MappingClassNamespace;
            outPut.Render(mappingBaseTemplate);
            foreach (var tableName in tableNames)
            {
                try
                {
                    var table = reader.GetTable(tableName);
                    var entityTemplate = new EntityTemplate(table, Formatter) {Namespace = EntityClassNamespace};
                    outPut.Render(entityTemplate);
                    var mappingTemplate = new MappingTemplate(table, Formatter)
                                              {
                                                  Namespace = MappingClassNamespace,
                                                  EntityClassNamespace = EntityClassNamespace
                                              };
                    outPut.Render(mappingTemplate);
                }
                catch (Exception)
                {
                    //throw;
                }
                
            }

        }
        
    }
}
